package com.admin.service.security.impl;

import cn.hutool.http.HttpUtil;
import com.admin.config.security.oa2.GiteeOAuth2User;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;

public class CustomOAuth2UserService  implements OAuth2UserService {
    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        String uri = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri();
            String tokenValue = userRequest.getAccessToken().getTokenValue();
            uri = uri + "?access_token=" + tokenValue;
            String result = HttpUtil.get(uri);
            OAuth2User giteeOAuth2User = JSONObject.parseObject(result, GiteeOAuth2User.class);
            return giteeOAuth2User;
    }
}
